import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Task {
	public static int nextThreadValue = 4;
	
	public static void main(String[] args) {
		 ExecutorService executor = Executors.newFixedThreadPool(4);
		 executor.execute(new Print(1));
		 executor.execute(new Print(2));
		 executor.execute(new Print(3));
		 executor.execute(new Print(4));
		 executor.shutdown();
	}
	

	public static class Print implements Runnable {
		private int value;
		private static Lock lock = new ReentrantLock();
		private static Condition condition = lock.newCondition();

		public Print(int value) {
			this.value = value;
		}

		public void run(){
			lock.lock();
			try{
				while(value != nextThreadValue){
					condition.await();
				}
				for(int i = 0; i<2; i++){
					System.out.println(value);
				}
				condition.signalAll();
				nextThreadValue--;
			} catch (InterruptedException e) {
				e.printStackTrace();
			} finally {
				lock.unlock();
			}
			
		}
	}
}
